﻿using Engine;
using Game.NetWork.Packages;
using Game.NetWork;
using Game.Server.Event;
using Game.Server.PlayerEvent;
using System.Text;
using GameEntitySystem;
using System.Collections.Generic;
using static Game.SubsystemCreatureSpawn;
using static Game.Server.Plugins.PegasusPVPZHPlugin;
using System.IO;
using Newtonsoft.Json;
using System.Security.Cryptography;
using System.Collections;
using System;

namespace Game.Server.Plugins
{
    public class CmdPegasus : AbstractProcessCmd
    {
        public override string Cmd => "peg";

        public override string Introduce => "/peg  -天马PVP";

        public override int AuthLevel => 100;

        public override DisplayType Display => DisplayType.Authority;

        public override void ProcessCmd()
        {
            if (GameManager.Project == null) return;
            if (m_messageDatas.Length >= 2)
            {
                if (m_isTerminal == true)
                {
                    SendMessage(Cmd, "控制台不能使用");
                    return;
                }
                try
                {
                    if (m_messageDatas[1] == "lock")
                    {
                        bool flag = m_client.PlayerData.ComponentPlayer.ComponentHealth.IsInvulnerable;
                        m_client.PlayerData.ComponentPlayer.ComponentHealth.IsInvulnerable = !flag;
                        SendMessage(Cmd, "已成功" + (!flag ? "开启锁血模式": "关闭锁血模式"));
                    }
                    else if(m_messageDatas[1] == "paw")
                    {
                        GameManager.Project.FindSubsystem<SubsystemGameInfo>().WorldSettings.Password = m_messageDatas[2];
                        SendMessage(Cmd, "密码设置成功");
                    }
                    else if (m_messageDatas[1] == "say")
                    {
                        GameManager.Project.FindSubsystem<SubsystemGameWidgets>().AddMessage(m_messageDatas[2]);
                    }
                }
                catch
                {
                    SendMessage(Cmd, "输入的指令有误，请重试");
                }
            }
            else
            {
                string help = "/peg  -天马PVP";
                SendMessage(Cmd, help);
            }
        }
    }

    public class PegasusPVPZHPlugin : ServerPlugin, IPlayerBreakAndPlaceHandle, IExplodeEventHandle, IFireEventHandle, IPlayerInteractEventHandle, IMessageEventHandle,ICreatureSpawnEventHandle, ICreatureHealthEventHandle
    {
        public class BlockDropItem
        {
            public int I;
            public int C;
            public float P;
        }

        public class RankItem
        {
            public string Name;
            public int Score;
            public int DieCount;
            public bool Online;
        }

        public class PegasusData
        {
            public Dictionary<string, string> Names = new Dictionary<string, string>();
            public Dictionary<string, int> DieCounts = new Dictionary<string, int>();
            public Dictionary<string, int> Scores = new Dictionary<string, int>();
        }

        public override int Version => 10000;

        public override string Name => "天马pvp插件";

        public byte FirstLevel => 0;

        public static string ConfigFilePath;

        public SubsystemCreatureSpawn m_subsystemCreatureSpawn;

        public SubsystemTerrain m_subsystemTerrain;

        public SubsystemProjectiles m_subsystemProjectiles;

        public SubsystemPickables m_subsystemPickables;

        public SubsystemPlayers m_subsystemPlayers;

        public SubsystemFireworksBlockBehavior m_subsystemFireworksBlockBehavior;

        public static PegasusData m_pegasusData;

        public static List<BlockDropItem> m_creatureDropItems = new List<BlockDropItem>();

        public static List<Vector3> m_spawnPoints = new List<Vector3>();

        public static Dictionary<string, int> FireCounts = new Dictionary<string, int>();

        public float m_exposeTime = 0f;

        public float m_locateTime = 0f;

        public Point3 m_locatePoint = Point3.Zero;

        public bool m_init = false;

        public string m_selectPlayerGuid = string.Empty;

        public override void Initialize()
        {
            PlayerBreakAndPlaceBlockEventManager.AddObject(this);
            ExplodeEventManager.AddObject(this);
            FireEventManager.AddObject(this);
            PlayerInteractEventManager.AddObject(this);
            MessageEventManager.AddObject(this);
            CreatureSpawnEventManager.AddObject(this);
            CreatureHealthEventManager.AddObject(this);
        }

        public bool PlayerBreakEvent(ComponentPlayer componentPlayer, Point3 point, int digBlockValue, int toolLevel)
        {
            if (!componentPlayer.PlayerData.ServerManager)
            {
                //DisplaySmallMessageNet(componentPlayer, "警告，不可破坏方块！");
                return false;
            }
            return true;
        }

        public bool PlayerPlaceEvent(ComponentPlayer componentPlayer, Point3 point, int placeBlockValue)
        {
            if (!componentPlayer.PlayerData.ServerManager)
            {
                DisplaySmallMessageNet(componentPlayer, "警告，不可放置方块！");
                return false;
            }
            return true;
        }

        public void Explode(int x, int y, int z, ref float pressure, bool isIncendiary, bool noExplosionSound, PlayerData miner)
        {
        }

        public bool Fire(Ray3 ray, ComponentMiner componentMiner)
        {
            return true;
        }

        public bool FireTerrain(CellFace cellFace, ComponentMiner componentMiner)
        {
            if (!componentMiner.ComponentPlayer.PlayerData.ServerManager)
            {
                DisplaySmallMessageNet(componentMiner.ComponentPlayer, "警告，不可放火！");
                return false;
            }
            return true;
        }

        public void ReceiveMessage(string playerName, NetNode netNode, Client From, string message, byte messageType, out bool External)
        {
            External = false;
        }

        public bool EditSignMessage(Point3 point, ComponentPlayer componentPlayer)
        {
            if (!componentPlayer.PlayerData.ServerManager)
            {
                DisplaySmallMessageNet(componentPlayer, "警告，不可编辑告示牌！");
                return false;
            }
            return true;
        }


        public bool Interact(ComponentPlayer componentPlayer, CellFace cellFace)
        {
            return true;
        }

        public bool Use(ComponentPlayer componentPlayer, object raycast, int activeBlockValue)
        {
            int id = Terrain.ExtractContents(activeBlockValue);
            if (id == 230 && raycast is TerrainRaycastResult)
            {
                Point3 point = ((TerrainRaycastResult)raycast).CellFace.Point;
                if (!componentPlayer.PlayerData.ServerManager)
                {
                    DisplaySmallMessageNet(componentPlayer, "不可使用锤子！已记录备案");
                    var client = componentPlayer.PlayerData.Client;
                    Log.Information($"玩家:{client.Nickname} 社区ID:{client.CommunityAccountId} 尝试在({point.ToString()})使用锤子");
                    CommonLib.Net.RemoveClient(client, "不可使用锤子！已记录备案");
                    return false;
                }
            }
            else if(id == 177)
            {
                int num = (new Random()).Int(50, 100);
                componentPlayer.ComponentHealth.Heal((float)num / 100f);
                CommonLib.Net.QueuePackage(new ComponentHealthPackage(componentPlayer.ComponentHealth));
                int activeSlotIndex = componentPlayer.ComponentMiner.Inventory.ActiveSlotIndex;
                int slotCount = componentPlayer.ComponentMiner.Inventory.GetSlotCount(activeSlotIndex);
                componentPlayer.ComponentMiner.Inventory.RemoveSlotItems(activeSlotIndex, 1);
                DisplaySmallMessageNet(componentPlayer, $"已回复{num}%的血量");
            }
            else if (id == 176)
            {
                int num = (new Random()).Int(30, 50);
                componentPlayer.ComponentHealth.Heal((float)num / 100f);
                CommonLib.Net.QueuePackage(new ComponentHealthPackage(componentPlayer.ComponentHealth));
                int activeSlotIndex = componentPlayer.ComponentMiner.Inventory.ActiveSlotIndex;
                componentPlayer.ComponentMiner.Inventory.RemoveSlotItems(activeSlotIndex, 1);
                DisplaySmallMessageNet(componentPlayer, $"已回复{num}%的血量");
            } 
            else if (id == 160)
            {
                string msg = GetRank(componentPlayer.PlayerGuid.ToString(), m_subsystemPlayers);
                MessagePackage messagePackage = new MessagePackage(componentPlayer.PlayerGuid.ToString(), msg, 0, null);
                messagePackage.To = componentPlayer.PlayerData.Client;
                CommonLib.Net.QueuePackage(messagePackage);
            }
            else if (id == 88 || id == 77)
            {
                int num = (new Random()).Int(5, 10);
                componentPlayer.ComponentHealth.Heal((float)num / 100f);
                CommonLib.Net.QueuePackage(new ComponentHealthPackage(componentPlayer.ComponentHealth));
                int activeSlotIndex = componentPlayer.ComponentMiner.Inventory.ActiveSlotIndex;
                componentPlayer.ComponentMiner.Inventory.RemoveSlotItems(activeSlotIndex, 1);
                DisplaySmallMessageNet(componentPlayer, $"已回复{num}%的血量");
            }
            else if (id == 117)
            {
                string msg = "【积分排名制】\n使用哨子可以查看排名。\n击杀动物可获得随机物资与10积分，\n击杀玩家可获得他的积分，\n死亡积分清零，重置初始物品，\n组队将根据人数降低击杀积分，同时降低击杀概率，\n击杀动物有概率在背包获得经验球，\n雪球可以无限发射，射程增加,\n经验球和钻石可以飞雷神传送,\n面包和面团可回血，点击屏幕使用,\n砖头三击必杀，投掷暴击，但是无法破甲";
                if(!string.IsNullOrEmpty(m_selectPlayerGuid))
                {
                    RankItem ownRankItem = new RankItem
                    {
                        Name = GetName(m_selectPlayerGuid),
                        Score = GetScore(m_selectPlayerGuid),
                        DieCount = GetDieCount(m_selectPlayerGuid),
                        Online = IsOnLine(m_selectPlayerGuid, m_subsystemPlayers)
                    };
                    msg += $"\n\n当前暴露玩家：\n{Display(ownRankItem)}";
                }
                MessagePackage messagePackage = new MessagePackage(componentPlayer.PlayerGuid.ToString(), msg, 0, null);
                messagePackage.To = componentPlayer.PlayerData.Client;
                CommonLib.Net.QueuePackage(messagePackage);
            }
            return true;
        }

        public bool Hit(ComponentPlayer componentPlayer, ComponentBody componentBody, Vector3 hitPoint, Vector3 hitDirection)
        {
            return true;
        }

        public bool Aim(ComponentPlayer componentPlayer, int activeBlockValue, Ray3 aim, AimState state)
        {
            int id = Terrain.ExtractContents(activeBlockValue);
            if (id == 248 || id == 111)
            {
                if (state == AimState.Completed)
                {
                    try
                    {
                        object result = Raycast(aim, componentPlayer.ComponentMiner);
                        if (result is TerrainRaycastResult)
                        {
                            CellFace cellFace = ((TerrainRaycastResult)result).CellFace;
                            componentPlayer.ComponentBody.Position = new Vector3(cellFace.Point) + new Vector3(0.5f) - aim.Direction * 2f;
                            componentPlayer.ComponentBody.Velocity = Vector3.Zero;
                            CommonLib.Net.QueuePackage(new ComponentPlayerPackage(componentPlayer, ComponentPlayerPackage.PlayerAction.PositionSet));
                        }
                        else if (result is BodyRaycastResult)
                        {
                            ComponentBody componentBody = ((BodyRaycastResult)result).ComponentBody;
                            componentPlayer.ComponentBody.Position = componentBody.Position - aim.Direction * 2f;
                            componentPlayer.ComponentBody.Velocity = Vector3.Zero;
                            CommonLib.Net.QueuePackage(new ComponentPlayerPackage(componentPlayer, ComponentPlayerPackage.PlayerAction.PositionSet));
                        }
                    }
                    catch
                    {
                    }
                }
            } 
            else if (id == 85)
            {
                if(state == AimState.InProgress)
                {
                    try
                    {
                        if (!FireCounts.ContainsKey(componentPlayer.PlayerGuid.ToString()))
                        {
                            FireCounts[componentPlayer.PlayerGuid.ToString()] = 0;
                        }
                        FireCounts[componentPlayer.PlayerGuid.ToString()] += 1;
                        if (FireCounts[componentPlayer.PlayerGuid.ToString()] > 10)
                        {
                            FireCounts[componentPlayer.PlayerGuid.ToString()] = 0;
                            Vector3 vector = componentPlayer.ComponentMiner.ComponentCreature.ComponentCreatureModel.EyePosition + componentPlayer.ComponentMiner.ComponentCreature.ComponentBody.Matrix.Right * 0.4f;
                            var v = Vector3.Normalize(aim.Direction * 10f);
                            if (componentPlayer.ComponentMiner.Inventory != null)
                            {
                                int activeSlotIndex = componentPlayer.ComponentMiner.Inventory.ActiveSlotIndex;
                                int slotCount = componentPlayer.ComponentMiner.Inventory.GetSlotCount(activeSlotIndex);
                                Block block = BlocksManager.Blocks[id];
                                if (slotCount > 0)
                                {
                                    float num2 = block.ProjectileSpeed;
                                    m_subsystemProjectiles.FireProjectile(activeBlockValue, vector, v * num2, (new Random()).Vector3(5f, 10f), componentPlayer.ComponentMiner.ComponentCreature);
                                }
                            }
                        }
                    }
                    catch
                    {
                    }
                  
                }
            }
            return true;
        }

        public bool Update(SubsystemCreatureSpawn subsystemCreatureSpawn, float dt)
        {
            m_exposeTime += dt;
            m_locateTime += dt;
            if (!m_init)
            {
                m_init = true;
               //初始化
            }
            if (m_exposeTime > 100f)
            {
                m_exposeTime = 0;
                int sumScore = 0;
                try
                {
                    foreach (var player in m_subsystemPlayers.ComponentPlayers)
                    {
                        sumScore += GetScore(player.PlayerGuid.ToString());
                    }
                    int s = (new Random()).Int(0, sumScore);
                    for (int i = 0; i < m_subsystemPlayers.ComponentPlayers.Count; i++)
                    {
                        var player = m_subsystemPlayers.ComponentPlayers[i];
                        s = s - GetScore(player.PlayerGuid.ToString());
                        if (s <= 0)
                        {
                            m_selectPlayerGuid = player.PlayerGuid.ToString();
                            GameManager.Project.FindSubsystem<SubsystemGameWidgets>().AddMessage($"猎杀时刻！暴露玩家：{player.PlayerData.Name}，可跟随指南针定位");
                            break;
                        }
                    }
                }
                catch
                {
                }
            }
            if (m_locateTime > 3f)
            {
                m_locateTime = 0;
                for (int i = 0; i < m_subsystemPlayers.ComponentPlayers.Count; i++)
                {
                    var player = m_subsystemPlayers.ComponentPlayers[i];
                    if (player.PlayerGuid.ToString() == m_selectPlayerGuid)
                    {
                        Point3 point = new Point3(player.ComponentBody.Position);
                        m_subsystemTerrain.ChangeCell(m_locatePoint.X, 253, m_locatePoint.Z, 0);
                        m_subsystemTerrain.ChangeCell(m_locatePoint.X, 254, m_locatePoint.Z, 0);
                        m_locatePoint = point;
                        m_subsystemTerrain.ChangeCell(m_locatePoint.X, 253, m_locatePoint.Z, 475208);
                        m_subsystemTerrain.ChangeCell(m_locatePoint.X, 254, m_locatePoint.Z, 167);
                    }
                }
            }
            return true;
        }

        public void OnEntityAdded(SubsystemCreatureSpawn subsystemCreatureSpawn, Entity entity)
        {

        }

        public void OnEntityRemoved(SubsystemCreatureSpawn subsystemCreatureSpawn, Entity entity)
        {
            
        }

        public void OnPlayerSpawned(PlayerData playerData, Entity playerEntity, Vector3 position)
        {
            ComponentPlayer componentPlayer = playerEntity.FindComponent<ComponentPlayer>();
            if(componentPlayer != null)
            {
                m_pegasusData.Names[componentPlayer.PlayerGuid.ToString()] = playerData.Name;
                AddDieCount(componentPlayer.PlayerGuid.ToString());
                int dieCount = GetDieCount(componentPlayer.PlayerGuid.ToString());
                DisplayLargeMessageNet(componentPlayer, $"累积死亡次数{dieCount}", "积分已清零，使用指南针可查看规则和玩法", 5f, 10f);
                componentPlayer.ComponentMiner.Inventory.AddSlotItems(0, 85, 999); //雪球
                componentPlayer.ComponentMiner.Inventory.AddSlotItems(1, 123, 1); //狼牙棒
                componentPlayer.ComponentMiner.Inventory.AddSlotItems(2, 248, 10); //经验球
                componentPlayer.ComponentMiner.Inventory.AddSlotItems(3, 201, 5 - dieCount); //远程炸弹
                componentPlayer.ComponentMiner.Inventory.AddSlotItems(4, 228, 5 - dieCount); //炸弹
                componentPlayer.ComponentMiner.Inventory.AddSlotItems(5, 177, 3 - dieCount); //面包
                componentPlayer.ComponentMiner.Inventory.AddSlotItems(11, 377035, 1); //木甲
                componentPlayer.ComponentMiner.Inventory.AddSlotItems(12, 475339, 1); //皮鞋
                componentPlayer.ComponentMiner.Inventory.AddSlotItems(15, 160, 1); //哨子
                componentPlayer.ComponentMiner.Inventory.AddSlotItems(16, 117, 1); //指南针
                int score = GetScore(componentPlayer.PlayerGuid.ToString());
                if(score >= 500 && dieCount < 10)
                {
                    ComponentClothing componentClothing = componentPlayer.ComponentMiner.Entity.FindComponent<ComponentClothing>();
                    if(componentClothing != null)
                    {
                        componentClothing.SetClothes(ClothingSlot.Torso, new List<int> { 557259 });
                        componentClothing.SetClothes(ClothingSlot.Legs, new List<int> { 573643 });
                        componentClothing.SetClothes(ClothingSlot.Feet, new List<int> { 590027 });
                        if(score >= 1000)
                        {
                            componentPlayer.ComponentMiner.Inventory.AddSlotItems(6, 111, 10); //钻石
                        }
                    }
                }
                ClearScore(componentPlayer.PlayerGuid.ToString());
                int i = (new Random()).Int(0, m_spawnPoints.Count - 1);
                componentPlayer.ComponentBody.Position = m_spawnPoints[i] + new Vector3(0.5f);
            }
        }

        public void InitCreatureTypes(SubsystemCreatureSpawn subsystemCreatureSpawn, List<CreatureType> creatureTypes)
        {
            Random m_random = new Random();
            List<CreatureType> m_creatureTypes = creatureTypes;
            m_creatureTypes.Clear();
            m_creatureTypes.Add(new CreatureType("Duck", SpawnLocationType.Surface, randomSpawn: true, constantSpawn: false)
            {
                SpawnSuitabilityFunction = delegate (CreatureType creatureType, Point3 point)
                {
                    return LightCondition(point);
                },
                SpawnFunction = (CreatureType creatureType, Point3 point) => SpawnCreatures(creatureType, "Duck", point, 1).Count
            });
            m_creatureTypes.Add(new CreatureType("Raven", SpawnLocationType.Surface, randomSpawn: true, constantSpawn: false)
            {
                SpawnSuitabilityFunction = delegate (CreatureType creatureType, Point3 point)
                {
                    return LightCondition(point);
                },
                SpawnFunction = (CreatureType creatureType, Point3 point) => SpawnCreatures(creatureType, "Raven", point, 1).Count
            });
            m_creatureTypes.Add(new CreatureType("Seagull", SpawnLocationType.Surface, randomSpawn: true, constantSpawn: false)
            {
                SpawnSuitabilityFunction = delegate (CreatureType creatureType, Point3 point)
                {
                    return LightCondition(point);
                },
                SpawnFunction = (CreatureType creatureType, Point3 point) => SpawnCreatures(creatureType, "Seagull", point, 1).Count
            });
            m_creatureTypes.Add(new CreatureType("Wildboar", SpawnLocationType.Surface, randomSpawn: false, constantSpawn: false)
            {
                SpawnSuitabilityFunction = delegate (CreatureType creatureType, Point3 point)
                {
                    return LightCondition(point);
                },
                SpawnFunction = (CreatureType creatureType, Point3 point) => SpawnCreatures(creatureType, "Wildboar", point, 1).Count
            });
            m_creatureTypes.Add(new CreatureType("Brown Cattle", SpawnLocationType.Surface, randomSpawn: false, constantSpawn: false)
            {
                SpawnSuitabilityFunction = delegate (CreatureType creatureType, Point3 point)
                {
                    return LightCondition(point);
                },
                SpawnFunction = delegate (CreatureType creatureType, Point3 point)
                {
                    int num87 = m_random.Int(3, 5);
                    int num88 = MathUtils.Min(m_random.Int(1, 3), num87);
                    int count2 = num87 - num88;
                    return 0 + SpawnCreatures(creatureType, "Bull_Brown", point, num88).Count + SpawnCreatures(creatureType, "Cow_Brown", point, count2).Count;
                }
            });
            m_creatureTypes.Add(new CreatureType("Black Cattle", SpawnLocationType.Surface, randomSpawn: false, constantSpawn: false)
            {
                SpawnSuitabilityFunction = delegate (CreatureType creatureType, Point3 point)
                {
                    return LightCondition(point);
                },
                SpawnFunction = delegate (CreatureType creatureType, Point3 point)
                {
                    int num83 = m_random.Int(3, 5);
                    int num84 = MathUtils.Min(m_random.Int(1, 3), num83);
                    int count = num83 - num84;
                    return 0 + SpawnCreatures(creatureType, "Bull_Black", point, num84).Count + SpawnCreatures(creatureType, "Cow_Black", point, count).Count;
                }
            });
            m_creatureTypes.Add(new CreatureType("White Bull", SpawnLocationType.Surface, randomSpawn: false, constantSpawn: false)
            {
                SpawnSuitabilityFunction = delegate (CreatureType creatureType, Point3 point)
                {
                    return LightCondition(point);
                },
                SpawnFunction = (CreatureType creatureType, Point3 point) => SpawnCreatures(creatureType, "Bull_White", point, 1).Count
            });
            m_creatureTypes.Add(new CreatureType("Gray Wolves", SpawnLocationType.Surface, randomSpawn: false, constantSpawn: false)
            {
                SpawnSuitabilityFunction = delegate (CreatureType creatureType, Point3 point)
                {
                    return LightCondition(point);
                },
                SpawnFunction = (CreatureType creatureType, Point3 point) => SpawnCreatures(creatureType, "Wolf_Gray", point, m_random.Int(1, 3)).Count
            });
            m_creatureTypes.Add(new CreatureType("Coyotes", SpawnLocationType.Surface, randomSpawn: false, constantSpawn: false)
            {
                SpawnSuitabilityFunction = delegate (CreatureType creatureType, Point3 point)
                {
                    return LightCondition(point);
                },
                SpawnFunction = (CreatureType creatureType, Point3 point) => SpawnCreatures(creatureType, "Wolf_Coyote", point, m_random.Int(1, 3)).Count
            });
            m_creatureTypes.Add(new CreatureType("Brown Bears", SpawnLocationType.Surface, randomSpawn: false, constantSpawn: false)
            {
                SpawnSuitabilityFunction = delegate (CreatureType creatureType, Point3 point)
                {
                    return LightCondition(point);
                },
                SpawnFunction = (CreatureType creatureType, Point3 point) => SpawnCreatures(creatureType, "Bear_Brown", point, 1).Count
            });
            m_creatureTypes.Add(new CreatureType("Black Bears", SpawnLocationType.Surface, randomSpawn: false, constantSpawn: false)
            {
                SpawnSuitabilityFunction = delegate (CreatureType creatureType, Point3 point)
                {
                    return LightCondition(point);
                },
                SpawnFunction = (CreatureType creatureType, Point3 point) => SpawnCreatures(creatureType, "Bear_Black", point, 1).Count
            });
            m_creatureTypes.Add(new CreatureType("Polar Bears", SpawnLocationType.Surface, randomSpawn: false, constantSpawn: false)
            {
                SpawnSuitabilityFunction = delegate (CreatureType creatureType, Point3 point)
                {
                    return LightCondition(point);
                },
                SpawnFunction = (CreatureType creatureType, Point3 point) => SpawnCreatures(creatureType, "Bear_Polar", point, 1).Count
            });
            m_creatureTypes.Add(new CreatureType("Horses", SpawnLocationType.Surface, randomSpawn: false, constantSpawn: false)
            {
                SpawnSuitabilityFunction = delegate (CreatureType creatureType, Point3 point)
                {
                    return LightCondition(point);
                },
                SpawnFunction = delegate (CreatureType creatureType, Point3 point)
                {
                    int temperature28 = m_subsystemTerrain.Terrain.GetTemperature(point.X, point.Z);
                    int num68 = 0;
                    if (m_random.Float(0f, 1f) < 0.35f)
                    {
                        num68 += SpawnCreatures(creatureType, "Horse_Black", point, 1).Count;
                    }
                    if (m_random.Float(0f, 1f) < 0.5f)
                    {
                        num68 += SpawnCreatures(creatureType, "Horse_Bay", point, 1).Count;
                    }
                    if (m_random.Float(0f, 1f) < 0.5f)
                    {
                        num68 += SpawnCreatures(creatureType, "Horse_Chestnut", point, 1).Count;
                    }
                    if (temperature28 > 8 && m_random.Float(0f, 1f) < 0.3f)
                    {
                        num68 += SpawnCreatures(creatureType, "Horse_Palomino", point, 1).Count;
                    }
                    if (temperature28 < 8 && m_random.Float(0f, 1f) < 0.3f)
                    {
                        num68 += SpawnCreatures(creatureType, "Horse_White", point, 1).Count;
                    }
                    return num68;
                }
            });
            m_creatureTypes.Add(new CreatureType("Camels", SpawnLocationType.Surface, randomSpawn: false, constantSpawn: false)
            {
                SpawnSuitabilityFunction = delegate (CreatureType creatureType, Point3 point)
                {
                    return LightCondition(point);
                },
                SpawnFunction = (CreatureType creatureType, Point3 point) => SpawnCreatures(creatureType, "Camel", point, m_random.Int(1, 2)).Count
            });
            m_creatureTypes.Add(new CreatureType("Donkeys", SpawnLocationType.Surface, randomSpawn: false, constantSpawn: false)
            {
                SpawnSuitabilityFunction = delegate (CreatureType creatureType, Point3 point)
                {
                    return LightCondition(point);
                },
                SpawnFunction = (CreatureType creatureType, Point3 point) => SpawnCreatures(creatureType, "Donkey", point, 1).Count
            });
            m_creatureTypes.Add(new CreatureType("Giraffes", SpawnLocationType.Surface, randomSpawn: false, constantSpawn: false)
            {
                SpawnSuitabilityFunction = delegate (CreatureType creatureType, Point3 point)
                {
                    return LightCondition(point);
                },
                SpawnFunction = (CreatureType creatureType, Point3 point) => SpawnCreatures(creatureType, "Giraffe", point, m_random.Int(1, 2)).Count
            });
            m_creatureTypes.Add(new CreatureType("Rhinos", SpawnLocationType.Surface, randomSpawn: false, constantSpawn: false)
            {
                SpawnSuitabilityFunction = delegate (CreatureType creatureType, Point3 point)
                {
                    return LightCondition(point);
                },
                SpawnFunction = (CreatureType creatureType, Point3 point) => SpawnCreatures(creatureType, "Rhino", point, 1).Count
            });
            m_creatureTypes.Add(new CreatureType("Tigers", SpawnLocationType.Surface, randomSpawn: false, constantSpawn: false)
            {
                SpawnSuitabilityFunction = delegate (CreatureType creatureType, Point3 point)
                {
                    return LightCondition(point);
                },
                SpawnFunction = (CreatureType creatureType, Point3 point) => SpawnCreatures(creatureType, "Tiger", point, 1).Count
            });
            m_creatureTypes.Add(new CreatureType("White Tigers", SpawnLocationType.Surface, randomSpawn: false, constantSpawn: false)
            {
                SpawnSuitabilityFunction = delegate (CreatureType creatureType, Point3 point)
                {
                    return LightCondition(point);
                },
                SpawnFunction = (CreatureType creatureType, Point3 point) => SpawnCreatures(creatureType, "Tiger_White", point, 1).Count
            });
            m_creatureTypes.Add(new CreatureType("Lions", SpawnLocationType.Surface, randomSpawn: false, constantSpawn: false)
            {
                SpawnSuitabilityFunction = delegate (CreatureType creatureType, Point3 point)
                {
                    return LightCondition(point);
                },
                SpawnFunction = (CreatureType creatureType, Point3 point) => SpawnCreatures(creatureType, "Lion", point, 1).Count
            });
            m_creatureTypes.Add(new CreatureType("Jaguars", SpawnLocationType.Surface, randomSpawn: false, constantSpawn: false)
            {
                SpawnSuitabilityFunction = delegate (CreatureType creatureType, Point3 point)
                {
                    return LightCondition(point);
                },
                SpawnFunction = (CreatureType creatureType, Point3 point) => SpawnCreatures(creatureType, "Jaguar", point, 1).Count
            });
            m_creatureTypes.Add(new CreatureType("Leopards", SpawnLocationType.Surface, randomSpawn: false, constantSpawn: false)
            {
                SpawnSuitabilityFunction = delegate (CreatureType creatureType, Point3 point)
                {
                    return LightCondition(point);
                },
                SpawnFunction = (CreatureType creatureType, Point3 point) => SpawnCreatures(creatureType, "Leopard", point, 1).Count
            });
            m_creatureTypes.Add(new CreatureType("Zebras", SpawnLocationType.Surface, randomSpawn: false, constantSpawn: false)
            {
                SpawnSuitabilityFunction = delegate (CreatureType creatureType, Point3 point)
                {
                    return LightCondition(point);
                },
                SpawnFunction = (CreatureType creatureType, Point3 point) => SpawnCreatures(creatureType, "Zebra", point, m_random.Int(1, 2)).Count
            });
            m_creatureTypes.Add(new CreatureType("Gnus", SpawnLocationType.Surface, randomSpawn: false, constantSpawn: false)
            {
                SpawnSuitabilityFunction = delegate (CreatureType creatureType, Point3 point)
                {
                    return LightCondition(point);
                },
                SpawnFunction = (CreatureType creatureType, Point3 point) => SpawnCreatures(creatureType, "Gnu", point, m_random.Int(1, 2)).Count
            });
            m_creatureTypes.Add(new CreatureType("Reindeers", SpawnLocationType.Surface, randomSpawn: false, constantSpawn: false)
            {
                SpawnSuitabilityFunction = delegate (CreatureType creatureType, Point3 point)
                {
                    return LightCondition(point);
                },
                SpawnFunction = (CreatureType creatureType, Point3 point) => SpawnCreatures(creatureType, "Reindeer", point, m_random.Int(1, 3)).Count
            });
            m_creatureTypes.Add(new CreatureType("Mooses", SpawnLocationType.Surface, randomSpawn: false, constantSpawn: false)
            {
                SpawnSuitabilityFunction = delegate (CreatureType creatureType, Point3 point)
                {
                    return LightCondition(point);
                },
                SpawnFunction = (CreatureType creatureType, Point3 point) => SpawnCreatures(creatureType, "Moose", point, m_random.Int(1, 1)).Count
            });
            m_creatureTypes.Add(new CreatureType("Bisons", SpawnLocationType.Surface, randomSpawn: false, constantSpawn: false)
            {
                SpawnSuitabilityFunction = delegate (CreatureType creatureType, Point3 point)
                {
                    return LightCondition(point);
                },
                SpawnFunction = (CreatureType creatureType, Point3 point) => SpawnCreatures(creatureType, "Bison", point, m_random.Int(1, 4)).Count
            });
            m_creatureTypes.Add(new CreatureType("Ostriches", SpawnLocationType.Surface, randomSpawn: false, constantSpawn: false)
            {
                SpawnSuitabilityFunction = delegate (CreatureType creatureType, Point3 point)
                {
                    return LightCondition(point);
                },
                SpawnFunction = (CreatureType creatureType, Point3 point) => SpawnCreatures(creatureType, "Ostrich", point, m_random.Int(1, 2)).Count
            });
            m_creatureTypes.Add(new CreatureType("Cassowaries", SpawnLocationType.Surface, randomSpawn: false, constantSpawn: false)
            {
                SpawnSuitabilityFunction = delegate (CreatureType creatureType, Point3 point)
                {
                    return LightCondition(point);
                },
                SpawnFunction = (CreatureType creatureType, Point3 point) => SpawnCreatures(creatureType, "Cassowary", point, 1).Count
            });
            m_creatureTypes.Add(new CreatureType("Hyenas", SpawnLocationType.Surface, randomSpawn: false, constantSpawn: false)
            {
                SpawnSuitabilityFunction = delegate (CreatureType creatureType, Point3 point)
                {
                    return LightCondition(point);
                },
                SpawnFunction = (CreatureType creatureType, Point3 point) => SpawnCreatures(creatureType, "Hyena", point, m_random.Int(1, 2)).Count
            });
            m_creatureTypes.Add(new CreatureType("Cave Bears", SpawnLocationType.Cave, randomSpawn: false, constantSpawn: false)
            {
                SpawnSuitabilityFunction = delegate (CreatureType creatureType, Point3 point)
                {
                    return LightCondition(point);
                },
                SpawnFunction = delegate (CreatureType creatureType, Point3 point)
                {
                    string templateName11 = (m_random.Int(0, 1) == 0) ? "Bear_Black" : "Bear_Brown";
                    return SpawnCreatures(creatureType, templateName11, point, 1).Count;
                }
            });
            m_creatureTypes.Add(new CreatureType("Cave Tigers", SpawnLocationType.Cave, randomSpawn: false, constantSpawn: false)
            {
                SpawnSuitabilityFunction = delegate (CreatureType creatureType, Point3 point)
                {
                    return LightCondition(point);
                },
                SpawnFunction = (CreatureType creatureType, Point3 point) => SpawnCreatures(creatureType, "Tiger", point, 1).Count
            });
            m_creatureTypes.Add(new CreatureType("Cave Lions", SpawnLocationType.Cave, randomSpawn: false, constantSpawn: false)
            {
                SpawnSuitabilityFunction = delegate (CreatureType creatureType, Point3 point)
                {
                    return LightCondition(point);
                },
                SpawnFunction = (CreatureType creatureType, Point3 point) => SpawnCreatures(creatureType, "Lion", point, 1).Count
            });
            m_creatureTypes.Add(new CreatureType("Cave Jaguars", SpawnLocationType.Cave, randomSpawn: false, constantSpawn: false)
            {
                SpawnSuitabilityFunction = delegate (CreatureType creatureType, Point3 point)
                {
                    return LightCondition(point);
                },
                SpawnFunction = (CreatureType creatureType, Point3 point) => SpawnCreatures(creatureType, "Jaguar", point, 1).Count
            });
            m_creatureTypes.Add(new CreatureType("Cave Leopards", SpawnLocationType.Cave, randomSpawn: false, constantSpawn: false)
            {
                SpawnSuitabilityFunction = delegate (CreatureType creatureType, Point3 point)
                {
                    return LightCondition(point);
                },
                SpawnFunction = (CreatureType creatureType, Point3 point) => SpawnCreatures(creatureType, "Leopard", point, 1).Count
            });
            m_creatureTypes.Add(new CreatureType("Cave Hyenas", SpawnLocationType.Cave, randomSpawn: false, constantSpawn: false)
            {
                SpawnSuitabilityFunction = delegate (CreatureType creatureType, Point3 point)
                {
                    return LightCondition(point);
                },
                SpawnFunction = (CreatureType creatureType, Point3 point) => SpawnCreatures(creatureType, "Hyena", point, 1).Count
            });
            m_creatureTypes.Add(new CreatureType("Bull Sharks", SpawnLocationType.Water, randomSpawn: false, constantSpawn: false)
            {
                SpawnSuitabilityFunction = (CreatureType creatureType, Point3 point) =>
                {
                    return LightCondition(point);
                },
                SpawnFunction = delegate (CreatureType creatureType, Point3 point)
                {
                    string templateName10 = "Shark_Bull";
                    return SpawnCreatures(creatureType, templateName10, point, 1).Count;
                }
            }); ;
            m_creatureTypes.Add(new CreatureType("Tiger Sharks", SpawnLocationType.Water, randomSpawn: false, constantSpawn: false)
            {
                SpawnSuitabilityFunction = (CreatureType creatureType, Point3 point) =>
                {
                    return LightCondition(point);
                },
                SpawnFunction = delegate (CreatureType creatureType, Point3 point)
                {
                    string templateName9 = "Shark_Tiger";
                    return SpawnCreatures(creatureType, templateName9, point, 1).Count;
                }
            });
            m_creatureTypes.Add(new CreatureType("Great White Sharks", SpawnLocationType.Water, randomSpawn: false, constantSpawn: false)
            {
                SpawnSuitabilityFunction = (CreatureType creatureType, Point3 point) =>
                {
                    return LightCondition(point);
                },
                SpawnFunction = delegate (CreatureType creatureType, Point3 point)
                {
                    string templateName8 = "Shark_GreatWhite";
                    return SpawnCreatures(creatureType, templateName8, point, 1).Count;
                }
            });
            m_creatureTypes.Add(new CreatureType("Barracudas", SpawnLocationType.Water, randomSpawn: false, constantSpawn: false)
            {
                SpawnSuitabilityFunction = (CreatureType creatureType, Point3 point) =>
                {
                    return LightCondition(point);
                },
                SpawnFunction = delegate (CreatureType creatureType, Point3 point)
                {
                    string templateName7 = "Barracuda";
                    return SpawnCreatures(creatureType, templateName7, point, 1).Count;
                }
            });
            m_creatureTypes.Add(new CreatureType("Bass_Sea", SpawnLocationType.Water, randomSpawn: false, constantSpawn: false)
            {
                SpawnSuitabilityFunction = (CreatureType creatureType, Point3 point) =>
                {
                    return LightCondition(point);
                },
                SpawnFunction = delegate (CreatureType creatureType, Point3 point)
                {
                    string templateName6 = "Bass_Sea";
                    return SpawnCreatures(creatureType, templateName6, point, 1).Count;
                }
            });
            m_creatureTypes.Add(new CreatureType("Bass_Freshwater", SpawnLocationType.Water, randomSpawn: false, constantSpawn: false)
            {
                SpawnSuitabilityFunction = delegate (CreatureType creatureType, Point3 point)
                {
                    return LightCondition(point);
                },
                SpawnFunction = delegate (CreatureType creatureType, Point3 point)
                {
                    string templateName5 = "Bass_Freshwater";
                    return SpawnCreatures(creatureType, templateName5, point, m_random.Int(1, 2)).Count;
                }
            });
            m_creatureTypes.Add(new CreatureType("Rays", SpawnLocationType.Water, randomSpawn: false, constantSpawn: false)
            {
                SpawnSuitabilityFunction = delegate (CreatureType creatureType, Point3 point)
                {
                    return LightCondition(point);
                },
                SpawnFunction = delegate (CreatureType creatureType, Point3 point)
                {
                    int num27 = 0;
                    int num28 = 0;
                    for (int i = point.X - 2; i <= point.X + 2; i++)
                    {
                        for (int j = point.Z - 2; j <= point.Z + 2; j++)
                        {
                            if (m_subsystemTerrain.Terrain.GetCellContents(point.X, point.Y, point.Z) == 18)
                            {
                                for (int num29 = point.Y - 1; num29 > 0; num29--)
                                {
                                    switch (m_subsystemTerrain.Terrain.GetCellContents(point.X, num29, point.Z))
                                    {
                                        case 2:
                                            num27++;
                                            break;
                                        case 7:
                                            num28++;
                                            break;
                                        default:
                                            continue;
                                    }
                                    break;
                                }
                            }
                        }
                    }
                    string templateName4 = (num27 >= num28) ? "Ray_Brown" : "Ray_Yellow";
                    return SpawnCreatures(creatureType, templateName4, point, 1).Count;
                }
            });
            m_creatureTypes.Add(new CreatureType("Piranhas", SpawnLocationType.Water, randomSpawn: false, constantSpawn: false)
            {
                SpawnSuitabilityFunction = delegate (CreatureType creatureType, Point3 point)
                {
                    return LightCondition(point);
                },
                SpawnFunction = delegate (CreatureType creatureType, Point3 point)
                {
                    string templateName3 = "Piranha";
                    return SpawnCreatures(creatureType, templateName3, point, m_random.Int(2, 4)).Count;
                }
            });
            m_creatureTypes.Add(new CreatureType("Orcas", SpawnLocationType.Water, randomSpawn: false, constantSpawn: false)
            {
                SpawnSuitabilityFunction = delegate (CreatureType creatureType, Point3 point)
                {
                    return LightCondition(point);
                },
                SpawnFunction = delegate (CreatureType creatureType, Point3 point)
                {
                    string templateName2 = "Orca";
                    return SpawnCreatures(creatureType, templateName2, point, 1).Count;
                }
            });
            m_creatureTypes.Add(new CreatureType("Belugas", SpawnLocationType.Water, randomSpawn: false, constantSpawn: false)
            {
                SpawnSuitabilityFunction = delegate (CreatureType creatureType, Point3 point)
                {
                    return LightCondition(point);
                },
                SpawnFunction = delegate (CreatureType creatureType, Point3 point)
                {
                    string templateName = "Beluga";
                    return SpawnCreatures(creatureType, templateName, point, 1).Count;
                }
            });
            m_creatureTypes.Add(new CreatureType("Constant Gray Wolves", SpawnLocationType.Surface, randomSpawn: false, constantSpawn: true)
            {
                SpawnSuitabilityFunction = delegate (CreatureType creatureType, Point3 point)
                {
                    return LightCondition(point);
                },
                SpawnFunction = (CreatureType creatureType, Point3 point) => SpawnCreatures(creatureType, "Wolf_Gray", point, m_random.Int(1, 3)).Count
            });
            m_creatureTypes.Add(new CreatureType("Constant Coyotes", SpawnLocationType.Surface, randomSpawn: false, constantSpawn: true)
            {
                SpawnSuitabilityFunction = delegate (CreatureType creatureType, Point3 point)
                {
                    return LightCondition(point);
                },
                SpawnFunction = (CreatureType creatureType, Point3 point) => SpawnCreatures(creatureType, "Wolf_Coyote", point, m_random.Int(1, 3)).Count
            });
            m_creatureTypes.Add(new CreatureType("Constant Brown Bears", SpawnLocationType.Surface, randomSpawn: false, constantSpawn: true)
            {
                SpawnSuitabilityFunction = delegate (CreatureType creatureType, Point3 point)
                {
                    return LightCondition(point);
                },
                SpawnFunction = (CreatureType creatureType, Point3 point) => SpawnCreatures(creatureType, "Bear_Brown", point, 1).Count
            });
            m_creatureTypes.Add(new CreatureType("Constant Black Bears", SpawnLocationType.Surface, randomSpawn: false, constantSpawn: true)
            {
                SpawnSuitabilityFunction = delegate (CreatureType creatureType, Point3 point)
                {
                    return LightCondition(point);
                },
                SpawnFunction = (CreatureType creatureType, Point3 point) => SpawnCreatures(creatureType, "Bear_Black", point, 1).Count
            });
            m_creatureTypes.Add(new CreatureType("Constant Polar Bears", SpawnLocationType.Surface, randomSpawn: false, constantSpawn: true)
            {
                SpawnSuitabilityFunction = delegate (CreatureType creatureType, Point3 point)
                {
                    return LightCondition(point);
                },
                SpawnFunction = (CreatureType creatureType, Point3 point) => SpawnCreatures(creatureType, "Bear_Black", point, 1).Count
            });
            m_creatureTypes.Add(new CreatureType("Constant Tigers", SpawnLocationType.Surface, randomSpawn: false, constantSpawn: true)
            {
                SpawnSuitabilityFunction = delegate (CreatureType creatureType, Point3 point)
                {
                    return LightCondition(point);
                },
                SpawnFunction = (CreatureType creatureType, Point3 point) => SpawnCreatures(creatureType, "Tiger", point, 1).Count
            });
            m_creatureTypes.Add(new CreatureType("Constant Lions", SpawnLocationType.Surface, randomSpawn: false, constantSpawn: true)
            {
                SpawnSuitabilityFunction = delegate (CreatureType creatureType, Point3 point)
                {
                    return LightCondition(point);
                },
                SpawnFunction = (CreatureType creatureType, Point3 point) => SpawnCreatures(creatureType, "Lion", point, m_random.Int(1, 2)).Count
            });
            m_creatureTypes.Add(new CreatureType("Constant Jaguars", SpawnLocationType.Surface, randomSpawn: false, constantSpawn: true)
            {
                SpawnSuitabilityFunction = delegate (CreatureType creatureType, Point3 point)
                {
                    return LightCondition(point);
                },
                SpawnFunction = (CreatureType creatureType, Point3 point) => SpawnCreatures(creatureType, "Jaguar", point, 1).Count
            });
            m_creatureTypes.Add(new CreatureType("Constant Leopards", SpawnLocationType.Surface, randomSpawn: false, constantSpawn: true)
            {
                SpawnSuitabilityFunction = delegate (CreatureType creatureType, Point3 point)
                {
                    return LightCondition(point);
                },
                SpawnFunction = (CreatureType creatureType, Point3 point) => SpawnCreatures(creatureType, "Leopard", point, 1).Count
            });
            m_creatureTypes.Add(new CreatureType("Constant Hyenas", SpawnLocationType.Surface, randomSpawn: false, constantSpawn: true)
            {
                SpawnSuitabilityFunction = delegate (CreatureType creatureType, Point3 point)
                {
                    return LightCondition(point);
                },
                SpawnFunction = (CreatureType creatureType, Point3 point) => SpawnCreatures(creatureType, "Hyena", point, m_random.Int(1, 2)).Count
            });
            m_creatureTypes.Add(new CreatureType("Pigeon", SpawnLocationType.Surface, randomSpawn: true, constantSpawn: false)
            {
                SpawnSuitabilityFunction = delegate (CreatureType creatureType, Point3 point)
                {
                    return LightCondition(point);
                },
                SpawnFunction = (CreatureType creatureType, Point3 point) => SpawnCreatures(creatureType, "Pigeon", point, 1).Count
            });
            m_creatureTypes.Add(new CreatureType("Sparrow", SpawnLocationType.Surface, randomSpawn: true, constantSpawn: false)
            {
                SpawnSuitabilityFunction = delegate (CreatureType creatureType, Point3 point)
                {
                    return LightCondition(point);
                },
                SpawnFunction = delegate (CreatureType creatureType, Point3 point)
                {
                    int count3 = m_random.Int(1, 2);
                    return SpawnCreatures(creatureType, "Sparrow", point, count3).Count;
                }
            });
        }

        public bool Heal(ComponentHealth componentHealth, float amount)
        {
            return true;
        }

        public bool Injure(ComponentHealth componentHealth, float amount, ComponentCreature attacker, string cause)
        {
            if (attacker != null && componentHealth != null)
            {
                ComponentPlayer player = attacker.Entity.FindComponent<ComponentPlayer>();
                ComponentPlayer player2 = componentHealth.Entity.FindComponent<ComponentPlayer>();
                if (player != null && componentHealth.Health > 0 && componentHealth.Health - amount <= 0)
                {
                    int getScore = 0;
                    bool plus = false;
                    int teamCount = GetTeamCount(player, m_subsystemPlayers);
                    if(player2 == null)
                    {
                        getScore = 10 / teamCount;
                        Vector3 p = componentHealth.Entity.FindComponent<ComponentBody>().Position + new Vector3(0, 0.5f, 0);
                        Random random = new Random();
                        int i = random.Int(0, m_creatureDropItems.Count - 1);
                        if (random.Bool(m_creatureDropItems[i].P / (float)teamCount))
                        {
                            m_subsystemPickables.AddPickable(m_creatureDropItems[i].I, m_creatureDropItems[i].C, p, null, null);
                        }
                        if (random.Bool(0.3f))
                        {
                            plus = true;
                            player.ComponentMiner.Inventory.AddSlotItems(25, 248, 1); 
                        }
                    }
                    else
                    {
                        getScore = GetScore(player2.PlayerGuid.ToString());
                    }
                    AddScore(player.PlayerGuid.ToString(), getScore, player.PlayerData.Name);
                    int totalScore = GetScore(player.PlayerGuid.ToString());
                    DisplayLargeMessageNet(player, $"获得积分{getScore}，当前总分{totalScore}", plus ? "本次击杀获得经验球1个,请查看背包" : "", plus ? 4 : 2) ;
                }
            }
            return true;
        }

        public override void Load()
        {
            ConfigFilePath = GetPluginDataByWorldPath("PegasusData.json");
            m_subsystemCreatureSpawn = GameManager.Project.FindSubsystem<SubsystemCreatureSpawn>();
            m_subsystemTerrain = GameManager.Project.FindSubsystem<SubsystemTerrain>();
            m_subsystemProjectiles = GameManager.Project.FindSubsystem<SubsystemProjectiles>();
            m_subsystemPickables = GameManager.Project.FindSubsystem<SubsystemPickables>();
            m_subsystemPlayers = GameManager.Project.FindSubsystem<SubsystemPlayers>();
            m_subsystemFireworksBlockBehavior = GameManager.Project.FindSubsystem<SubsystemFireworksBlockBehavior>();
            m_subsystemPlayers.GlobalSpawnPosition = new Vector3(92, 122, 127);
            BlocksManager.Blocks[248].MaxStacking = 100000000;
            BlocksManager.Blocks[248].ProjectileSpeed = 1000000f;
            BlocksManager.Blocks[85].MaxStacking = 100000000;
            BlocksManager.Blocks[85].ProjectileSpeed = 100f;
            BlocksManager.Blocks[85].DefaultProjectilePower = 2f;
            BlocksManager.Blocks[74].DefaultProjectilePower = 100f;
            BlocksManager.Blocks[74].DefaultMeleePower = 20f;
            BlocksManager.Blocks[201].ProjectileSpeed = 100f;
            BlocksManager.Blocks[201].DefaultExplosionPressure = 1000;
            BlocksManager.Blocks[228].DefaultExplosionPressure = 1000;
            for (int i = 1; i < BlocksManager.Blocks.Length; i++)
            {
                try
                {
                    Block block = BlocksManager.Blocks[i];
                    //block.DigResilience = float.PositiveInfinity;
                    block.ExplosionResilience = float.PositiveInfinity;
                    block.ProjectileResilience = float.PositiveInfinity;
                    block.FireDuration = 0;
                    block.DefaultDropCount = 0;
                    block.DefaultExperienceCount = 0;
                }
                catch
                {
                }
            }
            m_pegasusData = new PegasusData();
            if (File.Exists(ConfigFilePath))
            {
                using (StreamReader file = File.OpenText(ConfigFilePath))
                {
                    string jsonText = file.ReadToEnd();
                    m_pegasusData = JsonConvert.DeserializeObject<PegasusData>(jsonText);
                }
            }
            SetDropItems();
            SetSpawnPoints();
        }

        public override void Save()
        {
            m_subsystemTerrain.ChangeCell(m_locatePoint.X, 253, m_locatePoint.Z, 0);
            m_subsystemTerrain.ChangeCell(m_locatePoint.X, 254, m_locatePoint.Z, 0);
            string jsonText = JsonConvert.SerializeObject(m_pegasusData);
            using (FileStream fileStream = File.Open(ConfigFilePath, System.IO.FileMode.Create))
            {
                using (StreamWriter fileWrite = new StreamWriter(fileStream))
                {
                    fileWrite.Write(jsonText);
                    fileWrite.Flush();
                }
            }
        }

        public string GetPluginDataByWorldPath(string fileName)
        {
            string pluginDataPath = Storage.GetSystemPath("app:/PluginData");
            if (!Directory.Exists(pluginDataPath))
            {
                Directory.CreateDirectory(pluginDataPath);
            }
            string worldName = GameManager.Project.FindSubsystem<SubsystemGameInfo>().WorldSettings.Name;
            string dataByWorldPath = Storage.CombinePaths(pluginDataPath, worldName);
            if (!Directory.Exists(dataByWorldPath))
            {
                Directory.CreateDirectory(dataByWorldPath);
            }
            return Storage.CombinePaths(dataByWorldPath, fileName);
        }

        public void DisplaySmallMessageNet(ComponentPlayer componentPlayer, string text)
        {
            var messagePackget = new MessagePackage(null, text, 0, null);
            messagePackget.To = componentPlayer.PlayerData.Client;
            CommonLib.Net.QueuePackage(messagePackget);
        }

        public void DisplayLargeMessageNet(ComponentPlayer componentPlayer, string text, string more, float duration = 2, float delay = 0)
        {
            var messagePackget = new MessagePackage(text, more, duration, delay);
            messagePackget.To = componentPlayer.PlayerData.Client;
            CommonLib.Net.QueuePackage(messagePackget);
        }

        public List<Entity> SpawnCreatures(CreatureType creatureType, string templateName, Point3 point, int count)
        {
            return m_subsystemCreatureSpawn.SpawnCreatures(creatureType, templateName, point, count);
        }

        public float LightCondition(Point3 point)
        {
            int id = m_subsystemTerrain.Terrain.GetCellContents(point.X, point.Y - 1, point.Z);
            int light = m_subsystemTerrain.Terrain.GetCellLight(point.X, point.Y, point.Z);
            return id != 0 && light > 7 ? 0.1f : 0f;
        }

        public static void AddDieCount(string guid)
        {
            if (m_pegasusData.DieCounts.TryGetValue(guid, out int count))
            {
                m_pegasusData.DieCounts[guid] += 1;
            }
            else
            {
                m_pegasusData.DieCounts[guid] = 0;
            }
        }

        public static int GetDieCount(string guid)
        {
            if (m_pegasusData.DieCounts.TryGetValue(guid, out int count))
            {
                return count;
            }
            else
            {
                return 0;
            }
        }

        public static int GetTeamCount(ComponentPlayer componentPlayer, SubsystemPlayers subsystemPlayers)
        {
            if(subsystemPlayers.ServerGroups.TryGetValue(componentPlayer.PlayerData.GroupKey, out SubsystemPlayers.Group group))
            {
                return group.members.Count;
            }
            else
            {
                return 1;
            }
        }

        public static string GetName(string guid)
        {
            if (m_pegasusData.Names.TryGetValue(guid, out string value))
            {
                return value;
            }
            else
            {
                return "未知";
            }
        }

        public static bool IsOnLine(string guid, SubsystemPlayers subsystemPlayers)
        {
            for(int i = 0; i < subsystemPlayers.ComponentPlayers.Count; i++)
            {
                var player = subsystemPlayers.ComponentPlayers[i];
                if(player.PlayerGuid.ToString() == guid)
                {
                    return true;
                }
            }
            return false;
        }

        public static void AddScore(string guid, int score, string name)
        {
            if (m_pegasusData.Scores.TryGetValue(guid, out int value))
            {
                m_pegasusData.Scores[guid] += score;
            }
            else
            {
                m_pegasusData.Scores[guid] = score;
            }
            m_pegasusData.Names[guid] = name;
        }

        public static void ClearScore(string guid)
        {
            if (m_pegasusData.Scores.TryGetValue(guid, out int value))
            {
                m_pegasusData.Scores[guid] = 0;
            }
        }

        public static int GetScore(string guid)
        {
            if (m_pegasusData.Scores.TryGetValue(guid, out int value))
            {
                return value;
            }
            else
            {
                return 0;
            }
        }

        public static string GetRank(string guid, SubsystemPlayers subsystemPlayers)
        {
            List<RankItem> rankList = new List<RankItem>();
            foreach(var sc in m_pegasusData.Scores)
            {
                if(sc.Value > 0)
                {
                    rankList.Add(new RankItem 
                    { 
                        Name = GetName(sc.Key),
                        Score = GetScore(sc.Key),
                        DieCount = GetDieCount(sc.Key),
                        Online = IsOnLine(sc.Key, subsystemPlayers)
                    });
                }
            }
            for (int i = 0; i < rankList.Count; i++)
            {
                for (int j = 0; j < rankList.Count - 1 - i; j++)
                {
                    if (rankList[j].Score <= rankList[j + 1].Score)
                    {
                        RankItem temp = rankList[j];
                        rankList[j] = rankList[j + 1];
                        rankList[j + 1] = temp;
                    }
                }
            }
            RankItem ownRankItem = new RankItem
            {
                Name = GetName(guid),
                Score = GetScore(guid),
                DieCount = GetDieCount(guid),
                Online = IsOnLine(guid, subsystemPlayers)
            };
            string msg = $"你的个人信息：\n{Display(ownRankItem)}\n\n";
            if(rankList.Count == 0)
            {
                msg += "【暂无玩家上榜】";
            }
            else
            {
                msg += "【前30排名榜】";
                for(int i = 0; i < rankList.Count && i < 30; i++)
                {
                    msg += $"\n排名:{i + 1} | {Display(rankList[i])}";
                }
            }
            return msg;
        }

        public static string Display(RankItem rankItem)
        {
            return $"玩家:{rankItem.Name} | 积分:{rankItem.Score} | 死亡次数:{rankItem.DieCount} | " + (rankItem.Online ? "在线" : "离线");
        }

        public static object Raycast(Ray3 ray, ComponentMiner componentMiner)
        {
            float reach = 1000f;
            Vector3 creaturePosition = componentMiner.ComponentCreature.ComponentCreatureModel.EyePosition;
            Vector3 start = ray.Position;
            var direction = Vector3.Normalize(ray.Direction);
            Vector3 end = ray.Position + direction * 500f;
            Point3 startCell = Terrain.ToCell(start);
            BodyRaycastResult? bodyRaycastResult = componentMiner.m_subsystemBodies.Raycast(start, end, 0.35f, (ComponentBody body, float distance) =>
            {
                bool flag = Vector3.DistanceSquared(start + distance * direction, creaturePosition) <= reach * reach;
                bool flag2 = body.Entity != componentMiner.Entity && !body.IsChildOfBody(componentMiner.ComponentCreature.ComponentBody) && !componentMiner.ComponentCreature.ComponentBody.IsChildOfBody(body);
                bool flag3 = Vector3.Dot(Vector3.Normalize(body.BoundingBox.Center() - start), direction) > 0.7f;
                return (flag && flag2 && flag3) ? true : false;
            });
            MovingBlocksRaycastResult? movingBlocksRaycastResult = componentMiner.m_subsystemMovingBlocks.Raycast(start, end, extendToFillCells: true);
            TerrainRaycastResult? terrainRaycastResult = componentMiner.m_subsystemTerrain.Raycast(start, end, true, true, delegate (int value, float distance)
            {
                return true;
            });
            float num = bodyRaycastResult.HasValue ? bodyRaycastResult.Value.Distance : float.PositiveInfinity;
            float num2 = movingBlocksRaycastResult.HasValue ? movingBlocksRaycastResult.Value.Distance : float.PositiveInfinity;
            float num3 = terrainRaycastResult.HasValue ? terrainRaycastResult.Value.Distance : float.PositiveInfinity;
            if (num < num2 && num < num3)
            {
                return bodyRaycastResult.Value;
            }
            if (num2 < num && num2 < num3)
            {
                return movingBlocksRaycastResult.Value;
            }
            if (num3 < num && num3 < num2)
            {
                return terrainRaycastResult.Value;
            }
            return new Ray3(start, direction);
        }

        public static void SetDropItems()
        {
            m_creatureDropItems.Clear();
            float k = 20f;
            m_creatureDropItems.Add(new BlockDropItem
            {
                I = 85, //雪球
                C = 10,
                P = 0.1f * k
            });
            m_creatureDropItems.Add(new BlockDropItem
            {
                I = 111, //钻石
                C = 1,
                P = 0.001f * k
            });
            m_creatureDropItems.Add(new BlockDropItem
            {
                I = 74, //板砖
                C = 1,
                P = 0.001f * k
            });
            m_creatureDropItems.Add(new BlockDropItem
            {
                I = 108, //火柴
                C = 3,
                P = 0.1f * k
            });
            m_creatureDropItems.Add(new BlockDropItem
            {
                I = 123, //狼牙棒
                C = 1,
                P = 0.05f * k
            });
            m_creatureDropItems.Add(new BlockDropItem
            {
                I = 81, //石矛
                C = 1,
                P = 0.1f * k
            });
            m_creatureDropItems.Add(new BlockDropItem
            {
                I = 220, //铜矛
                C = 1,
                P = 0.05f * k
            });
            m_creatureDropItems.Add(new BlockDropItem
            {
                I = 125, //钻刀
                C = 1,
                P = 0.01f * k
            });
            m_creatureDropItems.Add(new BlockDropItem
            {
                I = 201, //远程炸弹
                C = 5,
                P = 0.03f * k
            });
            m_creatureDropItems.Add(new BlockDropItem
            {
                I = 228, //炸弹
                C = 5,
                P = 0.03f * k
            });
            m_creatureDropItems.Add(new BlockDropItem
            {
                I = 475339, //皮鞋
                C = 1,
                P = 0.08f * k
            });
            m_creatureDropItems.Add(new BlockDropItem
            {
                I = 475339, //皮鞋
                C = 1,
                P = 0.08f * k
            });
            m_creatureDropItems.Add(new BlockDropItem
            {
                I = 442571, //马靴
                C = 1,
                P = 0.05f * k
            });
            m_creatureDropItems.Add(new BlockDropItem
            {
                I = 377035, //木甲
                C = 1,
                P = 0.08f * k
            });
            m_creatureDropItems.Add(new BlockDropItem
            {
                I = 458955, //木腿
                C = 1,
                P = 0.08f * k
            });
            m_creatureDropItems.Add(new BlockDropItem
            {
                I = 540875, //铜帽
                C = 1,
                P = 0.01f * k
            });
            m_creatureDropItems.Add(new BlockDropItem
            {
                I = 557259, //铜甲
                C = 1,
                P = 0.01f * k
            });
            m_creatureDropItems.Add(new BlockDropItem
            {
                I = 573643, //铜腿
                C = 1,
                P = 0.01f * k
            });
            m_creatureDropItems.Add(new BlockDropItem
            {
                I = 590027, //铜鞋
                C = 1,
                P = 0.01f * k
            });
            m_creatureDropItems.Add(new BlockDropItem
            {
                I = 344267, //钻帽
                C = 1,
                P = 0.005f * k
            });
            m_creatureDropItems.Add(new BlockDropItem
            {
                I = 311499, //钻甲
                C = 1,
                P = 0.005f * k
            });
            m_creatureDropItems.Add(new BlockDropItem
            {
                I = 327883, //钻腿
                C = 1,
                P = 0.005f * k
            });
            m_creatureDropItems.Add(new BlockDropItem
            {
                I = 360651, //钻鞋
                C = 1,
                P = 0.005f * k
            });
            m_creatureDropItems.Add(new BlockDropItem
            {
                I = 177, //面包
                C = 1,
                P = 0.03f * k
            });
            m_creatureDropItems.Add(new BlockDropItem
            {
                I = 176, //面团
                C = 1,
                P = 0.1f * k
            });
        }

        public static void SetSpawnPoints()
        {
            m_spawnPoints.Clear();
            m_spawnPoints.Add(new Vector3(-20, 114, 604));
            m_spawnPoints.Add(new Vector3(-171, 81, 440));
            m_spawnPoints.Add(new Vector3(47, 59, 478));
            m_spawnPoints.Add(new Vector3(120, 59, 486));
            m_spawnPoints.Add(new Vector3(248, 148, 418));
            m_spawnPoints.Add(new Vector3(213, 72, 438));
            m_spawnPoints.Add(new Vector3(119, 59, 424));
            m_spawnPoints.Add(new Vector3(66, 59, 410));
            m_spawnPoints.Add(new Vector3(10, 74, 327));
            m_spawnPoints.Add(new Vector3(89, 60, 333));
            m_spawnPoints.Add(new Vector3(-86, 73, 319));
            m_spawnPoints.Add(new Vector3(-162, 67, 316));
            m_spawnPoints.Add(new Vector3(-187, 65, 319));
            m_spawnPoints.Add(new Vector3(-60, 59, 283));
            m_spawnPoints.Add(new Vector3(-7, 61, 234));
            m_spawnPoints.Add(new Vector3(-115, 66, 188));
            m_spawnPoints.Add(new Vector3(-220, 66, 208));
            m_spawnPoints.Add(new Vector3(-306, 78, 93));
            m_spawnPoints.Add(new Vector3(-190, 71, 91));
            m_spawnPoints.Add(new Vector3(-55, 70, 97));
            m_spawnPoints.Add(new Vector3(5, 100, 122));
            m_spawnPoints.Add(new Vector3(178, 130, 16));
            m_spawnPoints.Add(new Vector3(-17, 32, -13));
            m_spawnPoints.Add(new Vector3(-99, 98, -3));
            m_spawnPoints.Add(new Vector3(-290, 83, 96));
            m_spawnPoints.Add(new Vector3(-266, 79, 5));
            m_spawnPoints.Add(new Vector3(-136, 96, -183));
            m_spawnPoints.Add(new Vector3(-68, 76, -73));
            m_spawnPoints.Add(new Vector3(-98, 132, 70));
            m_spawnPoints.Add(new Vector3(94, 99, 41));
        }
    }
}